package me.daei.soundmeter.mail;

import android.app.Notification;
import android.content.Context;
import android.content.pm.PackageInstaller;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

import me.daei.soundmeter.World;

public class Mail {
    final String TAG = "Mail";
    Context context = null;
    Properties props = null;
    final String user = "baby_baichenyang@163.com";
    final String password = "chenyang123";
    public Mail(Context context)
    {
        this.context = context;
        props = new Properties();
        props.put("mail.smtp.host", "smtp.163.com");
    }
    public String sendMailMy(String addresses,String message){

        Session session = Session.getInstance(props, null);
        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom("baby_baichenyang@163.com");
            msg.setRecipients(Message.RecipientType.TO, addresses);
            msg.setSubject("晨阳在哭！！！");
            msg.setSentDate(new Date());
            msg.setText(message);
            Transport.send(msg, user, password);
            Log.i(TAG,"发送成功");

            //在子线程操作UI线程
            Looper.prepare();
            Toast.makeText(context,"发送成功", Toast.LENGTH_SHORT).show();
            Looper.loop();

            return "发送邮件成功！";
        } catch (MessagingException mex) {
            System.out.println("send failed, exception: " + mex);
            Looper.prepare();
            Toast.makeText(context,mex.toString(), Toast.LENGTH_SHORT).show();
            Looper.loop();
            return mex+"";
        }
    }
    public void send_testMail()
    {
        Properties props = new Properties();
        props.put("mail.smtp.host", "my-mail-server");
        Session session = Session.getInstance(props, null);
        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom("me@example.com");
            msg.setRecipients(Message.RecipientType.TO, "you@example.com");
            msg.setSubject("JavaMail hello world example");
            msg.setSentDate(new Date());
            msg.setText("Hello, world!\n");
            Transport.send(msg, "me@example.com", "my-password");
        } catch (MessagingException mex) {
            System.out.println("send failed, exception: " + mex);

        }
    }
}
